<!doctype html>
<html lang="zh-cn">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <link rel="shortcut icon" href="/favicon.ico" />
    <link href="/app/ai/css/bootstrap.min.css?v=5.3" rel="stylesheet">
    <link href="/app/ai/css/app.css?v=<?=ai_css_version()?>" rel="stylesheet">
    <script src="/app/ai/js/jquery.min.js"></script>
    <script src="/app/ai/js/bootstrap.bundle.min.js?v=5.3"></script>

    <title>用户登录</title>

</head>
<body class="light-bg" data-bs-theme="light">

<div class="container">
    <div class="row d-flex align-items-center justify-content-center">
        <div style="width:380px;padding:1.5rem;" class="my-4">
            <form method="post">
                <h3 class="mb-3">登录</h3>
                <div class="form-group">
                    <input type="text" name="username" class="form-control" placeholder="用户名或Email" required>
                </div>
                <div class="form-group">
                    <input type="password" name="password" class="form-control" placeholder="密码" required>
                </div>
                <div class="form-group d-flex justify-content-between">
                    <input type="text" name="image_code" class="form-control w-50" autocomplete="off" placeholder="验证码" required>
                    <img class="rounded" src="/app/user/captcha/image/login"/>
                </div>
                <div class="form-group">
                    <button type="submit" class="btn btn-primary btn-block w-100">登录</button>
                </div>
                <div class="form-group d-flex justify-content-between">
                    <a class="text-decoration-none text-primary" id="registerLink" href="/app/ai/user/register">没有账号？点这里注册</a>
                    <a class="text-decoration-none text-primary" href="/app/user/password/reset" target="_blank">找回密码</a>
                </div>
            </form>
        </div>
    </div>

</div>
<style>
    .container, .row {
        height: 100%;
    }
</style>

<script src="/app/user/js/webman.js"></script>

<script>
    $('form img').on('click', function (e) {
        e.stopPropagation();
        e.preventDefault();
        $(this).attr('src', '/app/user/captcha/image/login?r='+ Math.random());
        $('input[name="image_code"]').val('');
    });

    $('input').keyup(function () {
        $(this).removeClass('is-invalid');
    });

    $('form').submit(function(event) {
        event.preventDefault();
        $.ajax({
            url: "/app/user/login",
            type: "POST",
            dataType: 'json',
            data: $(this).serialize(),
            success: function (e) {
                if (e.code !== 0) {
                    let field = e.data ? e.data.field : false;
                    field !== 'image_code' && $('form img').trigger('click');
                    field && $('input[name="'+field+'"]').addClass('is-invalid').focus();
                    return webman.error(e.msg);
                }
                webman.success('登录成功', function () {
                    let url = new URL(window.location.href);
                    let redirect = url.searchParams.get('redirect');
                    location.href = redirect && redirect.startsWith('/') && !redirect.startsWith('//') ? redirect : '/app/user';
                    try {
                        window.parent.ai.switchModule('chat');
                        window.parent.ai.loadUserInfo();
                    } catch (e) {}
                });
            }
        });
    });

    $("#registerLink").attr('href', '/app/ai/user/register' + location.search);

    $(document).click(function () {
        try {window.parent.ai.hideAll();} catch (e) {}
    });
    try {
        $(document.body).attr("data-bs-theme", window.parent.ai.theme);
    } catch (e) {}
</script>

</body>
</html>
